package cn.jcet.gumall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.jcet.gumall.product.entity.AttrEntity;
import cn.jcet.gumall.product.service.AttrAttrgroupRelationService;
import cn.jcet.gumall.product.service.AttrService;
import cn.jcet.gumall.product.service.CategoryService;
import cn.jcet.gumall.product.vo.AttrGroupWithAttrsVo;
import cn.jcet.gumall.product.vo.AttrRealationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.jcet.gumall.product.entity.AttrGroupEntity;
import cn.jcet.gumall.product.service.AttrGroupService;
import cn.jcet.common.utils.PageUtils;
import cn.jcet.common.utils.R;


/**
 * 属性分组
 *
 * @author coder_yang
 * @email 1546656922@qq.com
 * @date 2020-06-28 20:36:21
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 注入三级分类
     */
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;


    /**
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R AddRealation(@RequestBody List<AttrRealationVo> realationVo){

        this.relationService.saveBatch(realationVo);
        return R.ok();
    }

    /**
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R whihAttr(@PathVariable("catelogId") Long catelogId){

      List<AttrGroupWithAttrsVo>  vos =  this.attrGroupService.attrGroupWithAttrsBycatelogId(catelogId);

    return R.ok().put("data",vos);

    }

    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noattrRalation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page= this.attrService.getNoRelation(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     * 根据分组id查询
     * /product/attrgroup/{attrgroupId}/attr/relation
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {

        List<AttrEntity> attrEntityList = this.attrService.findAttrRelation(attrgroupId);

        return R.ok().put("data",attrEntityList);
    }

    /**
     * /product/attrgroup/attr/relation/delete
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRealationVo [] attrRealationVos){

        this.attrService.delectRelation(attrRealationVos);
        return R.ok();
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        /**
         * 查询保存的catelogid的路径
         */
//        Long[] catelogPath = attrGroup.getCatelogPath();
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = this.categoryService.findByCateLogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
